<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body {
	background-color: white;
	font-size: 14px;
}

textare {
	font-size: 14px;
	font-weight: bold;
	color: #FF0000;
}

.btn {
	height: 25;
	BORDER-RIGHT: #7b9ebd 1px solid;
	PADDING-RIGHT: 2px;
	BORDER-TOP: #7b9ebd 1px solid;
	PADDING-LEFT: 2px;
	FONT-SIZE: 12px;
	FILTER: progid :          DXImageTransform.Microsoft.Gradient (        
		                                GradientType =         0,
		StartColorStr = 
		   
		   #ffffff, EndColorStr =         #cecfde );
	BORDER-LEFT: #7b9ebd 1px solid;
	CURSOR: hand;
	COLOR: black;
	PADDING-TOP: 2px;
	BORDER-BOTTOM: #7b9ebd 1px solid
}

.selectIp {
	height: 300px;
	width: 260px;
}

.blankDiv {
	height: 30px;
}

.preShow {
	background-color: #FAFAD2;
	width: 1200px;
	margin: 0 auto;
}

.tab {
	background-color: mintcream;
	width: 1200px;
	height: 300px;
	margin: 0 auto;
} 


.listdiv {
	background-color: mintcream;
	width: 1200px; 
	margin: 0 auto;
}

.header {
	height: 20px;
	width: 100%;
}

.content {
	border: 1px solid #CCCCFF;
	border-top: none;
	height: 280px;
}

.liStyle {
	cursor: pointer;
	height: 20px;
	width: 120px;
	float: left;
	background-color: white;
}

.show {
	background-color: mintcream;
	border: 1px solid #CCCCFF;
	border-bottom: none;
}

.hide {
	background-color: white;
	border-bottom: 1px solid #CCCCFF;
}

.ulHide {
	display: none;
}

.ulShow {
	display: block;
}


ul {
	list-style: none outside none;
}

* {
	margin: 0;
	padding: 0;
} 

</style>


<script type="text/javascript"
	src="http://ajax.microsoft.com/ajax/jquery
/jquery-1.4.min.js"></script>
<script type="text/javascript"> 

var ipArray=[["10.16.42.195:7080","10.16.42.195:7080"],["10.16.42.46:7080","10.16.42.46:7080"]];

var mbeanName=["MonitorManage","MonitorStatData","DefaultMonitorDataDispatcher","MemoryCache$MemoryScaner","DataBaseCache$DataBaseScaner"];
var mbeanMethodName=["getState","start","stop","destory"];
var beginQuery="http://";
var endQuery="/monitorFilter"; 
var namespace = "irisMonitor:name=";
var queryMethod="&mname=";
//填充IP选择器
function init(){
	for(var i=0 ; i < ipArray.length ; i++){
		var option = "";
		if(i==0){
		    option = "<option value='"+ipArray[i][0]+"' selected>"+ipArray[i][1]+"</option>";
		}else{
			option = "<option value='"+ipArray[i][0]+"'>"+ipArray[i][1]+"</option>";
		}
	    $("#preIpSelect").append(option);
	}
} 



monitorState=function getMonitorState(){
    $("#monitorTable").html("");
    $("#monitorTable").append("<tr><td>IP</td><td>mbeanname</td><td>methodname</td><td>value</td></tr>");
    var strTrBegin = "<tr>";
    var strTrEnd = "</tr>";
    var strTdBegin = "<td>";
    var strTdEnd = "</td>";
	for(var i=0 ; i < ipArray.length ; i++){
       var url = beginQuery + ipArray[i][0] + endQuery + "?jopr=" + namespace + mbeanName[0] + queryMethod + mbeanMethodName[0];
 
       //alert(url);

       var ip = ipArray[i][0];
	       $.ajax({
		       async: true,
	           url: url,
	           dataType: 'jsonp',
	           jsonp: 'callback',
	           success: function(json,url) { //返回的json数据
		    	    var mbeanname;
		    	    var methodname;
		    	    var value;
		    	    var ip;
	                var jsonObject = json || {};
	                mbeanname = jsonObject[0].mbeanname;
	                methodname = jsonObject[0].methodname;
	                ip = jsonObject[0].ip;
	                value = jsonObject[0].value;
	                var strTr = strTrBegin;
	                strTr = strTr + strTdBegin + ip +strTdEnd;
	     	        strTr = strTr + strTdBegin + mbeanname +strTdEnd;
	    	        strTr = strTr + strTdBegin + methodname +strTdEnd;
	                strTr = strTr + strTdBegin + value +strTdEnd;
	                strTr = strTr+strTrEnd;
	                $("#monitorTable").append(strTr);
	           },
	           timeout: 100
	       }); 
	  }  
}



//定时获取监控的状态
//setInterval(monitorState, 5000);





//列出所有的mbean,只列出相关域的
listmbeans=function listmbeans(){
    $("#listMbeanTable").html("");
    $("#listMbeanTable").append("<tr><td  width=10%>IP</td><td width=25%>type</td><td width=25% >mbeanname</td><td width=20%>opers</td><td  width=20%>attrs</td></tr>");
    var strTrBegin = "<tr>";
    var strTrEnd = "</tr>";
    var strTdBegin = "<td><div  style='overflow:auto;'>";
    var strTdEnd = "</div></td>";
    var htmlBr = "</br>";
    var htmlNbsp="&nbsp;";   

    var mbeandomain =  $("#mbeandomain").val();

    var ip = $("#ipSelect").text().trim();
    var splitRet = (ip+"").split(",");  
     
    if(!ip || splitRet.length>1){
    	alert("选择的IP地址必须为一个. ")
	return false;   
    }    
 

    //for(var i=0 ; i < ipArray.length ; i++){
       //var url = beginQuery + ipArray[i][0] + endQuery + "?jopr=" + namespace + mbeanName[0] + queryMethod + mbeanMethodName[0];

       var url = beginQuery + ip + endQuery + "?jopr=" + mbeandomain; 
 
      //alert(url); 

       $.ajax({
	       async: true,
           url: url,
           dataType: 'jsonp',
           jsonp: 'callback',
           success: function(json,url) { //返回的json数据
	    	   
	    	   
                var jsonObject = json || {}; 

                var mbeans = jsonObject[0].list; 
                var ip = jsonObject[0].ip; 

                for(var j=0;j<mbeans.length;j++){
		        var mbeanname = mbeans[j].mbeanname; 
		        var mtype = mbeans[j].mtype;
                        var mattrs = mbeans[j].mattrs;
                        var mopers = mbeans[j].mopers;
          
                        var mattersHtml = "" ;
                        
                        for(var num =0 ; num<mattrs.length ; num++){
                            if(mattrs[num]!=undefined){
		                mattersHtml = mattersHtml + "<a href=\"javascript:alert('description: "+mattrs[num].describe+", $$ value is : " + mattrs[num].value + "');\">"+mattrs[num].attrname + "</a>" + htmlBr;
			    }
				// mattrs[num].writable
				// mattrs[num].readable
                        }

			var mopersHtml  = "" ;
                        var htmlParams  = "" ;
                        for(var num =0 ; num<mopers.length ; num++){
                           if(mopers[num]!=undefined){
		                mopersHtml = mopersHtml + "<a href=\"javascript:alert('description: "+mopers[num].describe+"');\">"+mopers[num].opername + "</a>" + htmlNbsp + htmlBr;   
                                 
                                var params = mopers[num].params;
                              
               
				for(var pnum =0 ; pnum<params.length ; pnum++){
		                        mopersHtml = mopersHtml + htmlNbsp + htmlNbsp + params[pnum].type + htmlNbsp + htmlNbsp ;
                                        mopersHtml = mopersHtml + "<a href=\"javascript:alert('description: "+params[pnum].description+"');\">" + params[pnum].name + "</a>" + htmlBr;   
                                } 

                              mopersHtml = mopersHtml + htmlBr;
                           }
                        }

		        var strTr = strTrBegin;
		        strTr = strTr + strTdBegin + ip +strTdEnd;
                        strTr = strTr + strTdBegin + mtype +strTdEnd;
	     	        strTr = strTr + strTdBegin + mbeanname +strTdEnd;
	    	        strTr = strTr + strTdBegin + mopersHtml +strTdEnd; 
		        strTr = strTr + strTdBegin + mattersHtml +strTdEnd;
		        strTr = strTr+strTrEnd;
		        $("#listMbeanTable").append(strTr);
                }
           },
           timeout: 100
       }); 
   //}  
}






$(document).ready(function(){
  $("#getMonitorState").bind("click",monitorState);
  $("#queryMbeansByDomain").bind("click",listmbeans);

  
  $("#addIp").bind("click",function(){
       var newIP = $("#newIp").val();
	   var option = "";
	   option = "<option value='"+newIP+"' selected>"+newIP+"</option>";
		
	   $("#preIpSelect").append(option);
  }); 
  
  
  $("#listMonitorSwitch").bind("click",function(){
	    var ips = $("#ipSelect").text();
	    var splitRet = (ips+"").split(","); 
	    var queryString = "?qry=irisMonitor:name=MonitorManage";
	    if(splitRet.length>1){
	    	alert("IP地址只能选择一个. ")
		    return false;   
	    }    
   	    var url = beginQuery + splitRet[0].trim() + endQuery + queryString;
   	    var sFeatures = "height=600, width=810, scrollbars=yes, resizable=yes";
   	    //alert(url);
   	    window.open(url, 'ListMonitor', sFeatures );
   	    return;
  });

  $("#addSelectedIP").bind("click",function(){
	    $("#preIpSelect option:selected").each(function(){  
	          var option = "<option value='"+$(this).val()+"'>"+$(this).text()+"</option>";                   
	          $("#selectedIp").append(option);  
	          $(this).remove();  
	    });  

	    $("#ipSelect").text("");
	    $("#selectedIp option").each(function(){  
	    	  $("#ipSelect").append($(this).val()+" ");
	    }); 
  });

  $("#deleteSelectedIP").bind("click",function(){
      $("#selectedIp option:selected").each(function(){  
          var option = "<option value='"+$(this).val()+"'>"+jQuery(this).text()+"</option>";                   
          $("#preIpSelect").append(option);  
          $(this).remove();
      });  

      $("#ipSelect").text("");
      $("#selectedIp option").each(function(){  
    	  $("#ipSelect").append($(this).val()+" ");
      }); 
});
  
  $("#preIpSelect").dblclick(function(){  
      $("#preIpSelect option:selected").each(function(){  
          var option = "<option value='"+jQuery(this).val()+"'>"+jQuery(this).text()+"</option>";                   
          $("#selectedIp").append(option);  
          $(this).remove();  
      });  

      $("#ipSelect").text("");
      $("#selectedIp option").each(function(){  
    	  $("#ipSelect").append($(this).val()+" ");
      }); 
  }); 


  $("#selectedIp").dblclick(function(){  
      $("#selectedIp option:selected").each(function(){  
          var option = "<option value='"+$(this).val()+"'>"+jQuery(this).text()+"</option>";                   
          $("#preIpSelect").append(option);  
          $(this).remove();
      });  

      $("#ipSelect").text("");
      $("#selectedIp option").each(function(){  
    	  $("#ipSelect").append($(this).val()+" ");
      }); 
  }); 
  
  
  $("#listConfigure").bind("click",function(){
	    var ips = $("#ipSelect").text().trim();
	    var splitRet = (ips+"").split(" ");
	    var queryString = "?qry=irisMonitor:name=MonitorConfigure";
	    if(splitRet.length>1){
	        alert("IP地址只能选择一个. ")
		    return false;   
	    }    
   	    var url = beginQuery + splitRet[0].trim() + endQuery + queryString;
   	    var sFeatures = "height=600, width=810, scrollbars=yes, resizable=yes";
   	    window.open(url, 'ListMbean', sFeatures );
   	    return;
  }); 
  

  $("#queryConfigure").bind("click",function(){
        var attname=$("#attName").val();
        if(attname==null || attname ==""){
        	alert("属性名必填. ")
  		    return false;  
        }
                
	    var ips = $("#ipSelect").text().trim();
	    var splitRet = (ips+"").split(" ");
	    var queryString = "?get=irisMonitor:name=MonitorConfigure";
	    var att = "&att=";
	    if(splitRet.length>1){
		    alert("IP地址只能选择一个. ")
		    return false;   
	    }    
   	    var url = beginQuery + splitRet[0].trim() + endQuery + queryString + att + attname;
   	    var sFeatures = "height=600, width=810, scrollbars=yes, resizable=yes";
   	    window.open(url, 'ListMbean', sFeatures );
 	    return;
  });

  $("#setConfigure").bind("click",function(){
	    var attname=$("#attName").val();
	    var attvalue=$("#attValue").val();
	    if(attname==null || attname ==""){
	      	alert("属性名必填. ")
			return false;  
	    }

	    if(attvalue==null || attvalue ==""){
	      	alert("属性值必填. ")
			return false;  
	    }
	    
	    var ips = $("#ipSelect").text().trim();
	    var splitRet = (ips+"").split(" ");
	    var queryString = "?set=irisMonitor:name=MonitorConfigure";
	    var att = "&att=";
	    var value = "&val=";
	    var url = beginQuery + splitRet[0].trim() + endQuery + queryString + att + attname + value + attvalue;
	    alert(url);
	     $.ajax({
	      url: url,
	      type: "GET",
	      timeout: 10000,
	      tryCount : 0,
	      retryLimit : 3,
	      cache: false,
	      success: function(retHtml) {
	            alert("set ok!" + retHtml);
	        },
	        error: function() {
	            alert("超时");
	        }
	    }); 
	    return;
   });

  $("#setAllConfigure")
	.bind(
			"click",
			function() {
				var attname = $("#attName").val();
				var attvalue = $("#attValue").val();
				if (attname == null || attname == "") {
					alert("属性名必填. ")
					return false;
				}

				if (attvalue == null || attvalue == "") {
					alert("属性值必填. ")
					return false;
				}

				var ips = $("#ipSelect").text().trim();
				var splitRet = (ips + "").split(" ");
				var queryString = "?set=irisMonitor:name=MonitorConfigure";
				var att = "&att=";
				var value = "&val=";

				for (i = 0; i < splitRet.length; i++) {
					var url = beginQuery
							+ splitRet[i].trim()
							+ endQuery + queryString
							+ att + attname + value
							+ attvalue;
					alert(url);
					$.ajax( {
						url : url,
						type : "GET",
						timeout : 10000,
						tryCount : 0,
						retryLimit : 3,
						cache : false,
						success : function(retHtml) {
							// alert("set ok!" +
							// retHtml);
					},
					error : function() {
						alert("超时");
					}
					});
				}
				return;
			});
   
   $("#mbNoParaMehodExcute").bind("click",function(){
          var ips = $("#ipSelect").text().trim();
	    var splitRet = (ips+"").split(" "); 
	    if(splitRet.length>1){
	    	alert("IP地址只能选择一个. ")
		    return false;   
	    }    
	    var mbname=document.getElementById("mbNameSelect").value;
	    if(mbname==null||mbname==""){
	      alert("请选择mb");
	    }
	   var mbMethodName=document.getElementById("mbMethodSelect").value;
	     if(mbMethodName==null||mbMethodName==""){
	      alert("请选择mb的方法");
	    }
	    var queryString = "?opr=irisMonitor:name="+mbname+"&mname="+mbMethodName;
	    var url = beginQuery + splitRet[0].trim() + endQuery + queryString;
   	    var sFeatures = "height=600, width=810, scrollbars=yes, resizable=yes";
   	    alert(url);
   	    window.open(url, 'excuteMbMethod', sFeatures );
   	    return;
  });
  
  
  
 $("#startAllMonitorMb").bind("click",function(){
	 for(var i=0 ; i < ipArray.length ; i++){
       var url = beginQuery + ipArray[i][0] + endQuery + "?jopr=" + namespace + mbeanName[0] + queryMethod + mbeanMethodName[1];
       alert(url);
       var ip = ipArray[i];
	       $.ajax({
		       async: true,
	           url: url,
	           dataType: 'jsonp',
	           jsonp: 'callback',
	           success: function(json,url) { //返回的json数据
		    	},
	           timeout: 100
	       }); 
	  }  
  });
  
  
   $("#stopAllMonitorMb").bind("click",function(){
		for(var i=0 ; i < ipArray.length ; i++){
       var url = beginQuery + ipArray[i][0] + endQuery + "?jopr=" + namespace + mbeanName[0] + queryMethod + mbeanMethodName[2];
       var ip = ipArray[i];
	       $.ajax({
		       async: true,
	           url: url,
	           dataType: 'jsonp',
	           jsonp: 'callback',
	           success: function(json,url) { //返回的json数据
		    	},
	           timeout: 100
	       }); 
	  }  
  });


   $("#destroyAllMonitorMb").bind("click",function(){
		for(var i=0 ; i < ipArray.length ; i++){
      var url = beginQuery + ipArray[i][0] + endQuery + "?jopr=" + namespace + mbeanName[0] + queryMethod + mbeanMethodName[3];
      var ip = ipArray[i];
	       $.ajax({
		       async: true,
	           url: url,
	           dataType: 'jsonp',
	           jsonp: 'callback',
	           success: function(json,url) { //返回的json数据
		    	},
	           timeout: 100
	       }); 
	  }  
 });
  
  
  $("#startOneIpMonitorMb").bind("click",function(){
	 var ips = $("#ipSelect").text().trim();
	    var splitRet = (ips+"").split(" ");
	   	if(splitRet.length>1){
	        alert("IP地址只能选择一个. ")
		    return false;   
	    }    
       var url = beginQuery + splitRet[0].trim() + endQuery + "?jopr=" + namespace + mbeanName[0] + queryMethod + mbeanMethodName[1];
       var sFeatures = "height=600, width=810, scrollbars=yes, resizable=yes";
   	    alert(url);
   	    window.open(url, 'startOneIpMonitorMb', sFeatures );
   	    return;
  });
  
  
   $("#stopOneIpMonitorMb").bind("click",function(){
		 var ips = $("#ipSelect").text().trim();
	    var splitRet = (ips+"").split(" ");
	    if(splitRet.length>1){
	        alert("IP地址只能选择一个. ")
		    return false;   
	    }    
       var url = beginQuery + splitRet[0].trim() + endQuery + "?jopr=" + namespace + mbeanName[0] + queryMethod + mbeanMethodName[2];
      
        var sFeatures = "height=600, width=810, scrollbars=yes, resizable=yes";
   	    alert(url);
   	    window.open(url, 'stopOneIpMonitorMb', sFeatures );
   	    return;
      
	 
  });

   
	  
	   $("#destroyOneIpMonitorMb").bind("click",function(){
			 var ips = $("#ipSelect").text().trim();
		    var splitRet = (ips+"").split(" ");
		    if(splitRet.length>1){
		        alert("IP地址只能选择一个. ")
			    return false;   
		    }    
	       var url = beginQuery + splitRet[0].trim() + endQuery + "?jopr=" + namespace + mbeanName[0] + queryMethod + mbeanMethodName[3];
	      
	        var sFeatures = "height=600, width=810, scrollbars=yes, resizable=yes";
	   	    alert(url);
	   	    window.open(url, 'stopOneIpMonitorMb', sFeatures );
	   	    return;
	      
		 
	  });

    
  
 
});

 

var mbMethodName=[
["getState","getRuntimeData"],
["getState","getScheduledFutureState","getScheduledFutureState"],
["getState","getScheduledFutureState","getScheduledFutureState"],
["getState","getScheduledFutureState","getScheduledFutureState"]
];

function getMbMethodName(){
         //获得对应Mb的对象
         var sltMb=document.getElementById("mbNameSelect");
         //获得mb方法下拉框的对象
         var sltMethod=document.getElementById("mbMethodSelect");
         
         //得到对应Mb的方法数组
         var mbMethod=mbMethodName[sltMb.selectedIndex - 1];
 
         //清空mb方法下拉框，仅留提示选项
         sltMethod.length=1;
 
         //将方法数组中的值填充到mb下拉框中
         for(var i=0;i<mbMethod.length;i++){
             sltMethod[i+1]=new Option(mbMethod[i],mbMethod[i]);
         }
     }

function tabinit() {           
            $(".tab > ul.header > li").click(function () {
                $(this).parent().find("li.show").addClass("hide").removeClass("show");  
                $(this).addClass("show").removeClass("hide");  
                var parentsEl = $(this).parents(".tab");  
                parentsEl.find("div > ul.ulShow").addClass("ulHide").removeClass("ulShow");  
                /*页面静态内容*/  
                var ary = parentsEl.find("ul.header > li");  
                parentsEl.find("div > ul:eq(" + $.inArray(this, ary) +")").addClass("ulShow").removeClass("ulHide");      
                /*用ajax动态加载内容  
                parentsEl.find("div > ul:eq(" + $.inArray(this, ary) + ")").addClass(function () {  
                    var el = this;  
                    $.post("TreeDataServlet", {param: "params"}, function(data) {  
                        $(el).html(data);  
                    });  
                    return "ulShow";  
                }).removeClass("ulHide");*/       
            })  
        } 

</script>



</head>
<body>
<div class="preShow">
<table>
	<tr>
		<td colspan=2>选择集群地址：</td>
	</tr>
	<tr>

		<td><font color=red> 已选中地址： </font> <lable> <textare
			id="ipSelect" name="ipSelect"></textare> </lable></td>
	</tr>
	<tr>
		<td colspan=2></td>
	</tr>
</table>

<table border=0>
	<tr>
		<td colspan="2" valign="top" width="47%"><select class="selectIp"
			multiple="multiple" id="preIpSelect" name="preIpSelect">
		</select></td>
		<td align="center"><input class="btn" type="button"
			id="addSelectedIP" name="addSelectedIP" value="选择" /></br>
		<input class="btn" type="button" id="deleteSelectedIP"
			name="deleteSelectedIP" value="删除" /></td>
		<td colspan="2" valign="top" width="47%"><select class="selectIp"
			multiple="multiple" id="selectedIp" name="selectedIp">
		</select></td>
	</tr>
</table>
</div>

<div class="listdiv">
  <br> 		
<input class="btn" type="text" id="mbeandomain"
			name="mbeandomain" value="dragonflyclient:*" />
<input class="btn" type="button" id="queryMbeansByDomain"
			name="queryMbeansByDomain" value="查询mbean" />    <br> 	   mbeans 列表: <br>
  
        <table id="listMbeanTable" border=1 width=100% style="table-layout:fixed">
	</table>

 <br>
 <br>
</div>




<div class="tab">
<ul class="header">
	<li class="liStyle show">集群监控状态</li> 
	<li class="liStyle hide">监控开关</li>
	<li class="liStyle hide">新增集群IP</li>
</ul>
<div class="content">
<ul class="ulShow">
	<br />
	集群监控目前状况：
	<table id="monitorTable" border=1>
	</table>
	<input class="btn" type="button" id="getMonitorState"
		name="getMonitorState" value="获取状态" />
</ul> 
<ul class="ulHide">
	<br />
	<table>
		<tr>
			<th>开启监控</th>
			<th>停止监控</th>
			<th>注销监控</th>
		</tr>
		<tr>
			<td><input class="btn" type="button" id="startAllMonitorMb"
				name="startAllMonitorMb" value="start所有监控" /></td>
			<td><input class="btn" type="button" id="stopAllMonitorMb"
				name="stopAllMonitorMb" value="stop所有监控" /></td>
			<td><input class="btn" type="button" id="destroyAllMonitorMb"
				name="destroyAllMonitorMb" value="destroy所有监控" /></td>
		</tr>
		<tr>
			<td><input class="btn" type="button" id="startOneIpMonitorMb"
				name="startOneIpMonitorMb" value="start选中ip的监控" /></td>
			<td><input class="btn" type="button" id="stopOneIpMonitorMb"
				name="stopOneIpMonitorMb" value="stop选中ip的监控" /></td>
			<td><input class="btn" type="button" id="destroyOneIpMonitorMb"
				name="destroyOneIpMonitorMb" value="destroy选中ip的监控" /></td>
		</tr>
	</table>
</ul>

<ul class="ulHide">
	<br />
	新增IP地址:
	<input class="btn" type="text" id="newIp" name="newIp" value="" />
	<br />
	<input class="btn" type="button" id="addIp" name="addIp" value="添加" />
</ul>
</div>
</div>
</body>
</html>


<script type="text/javascript">
//最后初始化
init();
tabinit();
//monitorState();
</script>
